package animix.blue.server.log.mapper;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class LogOperationProvider {

    public String list(Map<String, Object> para) {
        return new SQL() {{
            SELECT("*");
            FROM("tb_log_operation");
            if (StringUtils.isNotBlank((String) para.get("username"))) {
                WHERE("username like '%" + para.get("username") + "%'");
            }
            if (StringUtils.isNotBlank((String) para.get("action"))) {
                WHERE("action = '" + para.get("action") + "'");
            }
            if (StringUtils.isNotBlank((String) para.get("object"))) {
                WHERE("object = '" + para.get("object") + "'");
            }
            if (StringUtils.isNotBlank((String) para.get("operatedSt"))) {
                WHERE("unix_timestamp(operated)*1000 >= '" + para.get("operatedSt") + "'");
            }
            if (StringUtils.isNotBlank((String) para.get("operatedEd"))) {
                WHERE("unix_timestamp(operated)*1000 <= '" + para.get("operatedEd") + "'");
            }
            if (StringUtils.isNotBlank((String) para.get("sorter"))) {
                String[] sorter = ((String) para.get("sorter")).split("_");
                ORDER_BY(sorter[1].equals("descend") ? sorter[0] + " DESC" : sorter[0] + " ASC");
            } else {
                ORDER_BY("id DESC");
            }
        }}.toString();
    }
}
